﻿using System;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Reflection;

namespace Data
{
    //Lottery
    public class Documentary
    {
        /// <summary>
        /// 主键
        /// </summary>
        private readonly string primaryKey = "id";
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public long Add(Model.Documentary model)
        {
            return new DataAccess().Add<Model.Documentary>(model, primaryKey);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(Model.Documentary model)
        {
            return new DataAccess().Update<Model.Documentary>(model, primaryKey);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool Delete(long Id)
        {
            return new DataAccess().Delete<Model.Documentary>(Id, primaryKey);
        }
        /// <summary>
        /// 单条
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Model.Documentary GetModel(long Id)
        {
            return new DataAccess().Get<Model.Documentary>(Id, primaryKey);
        }
        public Model.Documentary GetModelByDocumentaryid(long Documentaryid)
        {
            return new DataAccess().Get<Model.Documentary>(Documentaryid, "Documentaryid");
        }
        public Model.Documentary GetModelBysql(string sql)
        {
            return new DataAccess().Get<Model.Documentary>(sql);
        }
        /// <summary>
        /// 总数据数
        /// </summary>
        /// <returns></returns>
        public int GetAll_Count()
        {
            return new DataAccess().GetCount<Model.Documentary>(primaryKey);
        }
        /// <summary>
        /// 抽取分页数据
        /// </summary>
        public DataTable GetPageListBysql(string sql)
        {
            return DbHelp.ExecuteDataset(DBConfig.ConnectionSettings, CommandType.Text, sql, null).Tables[0];

        }

        public List<Model.Documentary> GetPageList_Where(int PageIndex, int PageSize, long newsclassid, out int totalCount)
        {
            string strWhere = GetSqlWhere(newsclassid);
            totalCount = new DataAccess().GetCount<Model.Documentary>(primaryKey, strWhere);
            return new DataAccess().GetPage_List<Model.Documentary>(PageIndex, PageSize, primaryKey, primaryKey + " desc", strWhere);
        }




        public List<Model.Documentary> GetPageListWherezdf(int PageIndex, int PageSize, string sql, out int totalCount)
        {
            totalCount = new DataAccess().GetCount<Model.Documentary>(primaryKey, sql);
            return new DataAccess().GetPage_List<Model.Documentary>(PageIndex, PageSize, primaryKey, primaryKey + " desc", sql);
        }


        public List<Model.Documentary> GetPageListWhere(int PageIndex, int PageSize, string sql, out int totalCount)
        {
            totalCount = new DataAccess().GetCount<Model.Documentary>(primaryKey, sql);
            return new DataAccess().GetPage_List<Model.Documentary>(PageIndex, PageSize, primaryKey, primaryKey + " desc", sql);
        }
        public List<Model.Documentary> GetPageListWhere(int PageIndex, int PageSize,  int LotteryChippedid, out int totalCount)
        {
            string strWhere = GetSqlWhere(LotteryChippedid);
            totalCount = new DataAccess().GetCount<Model.Documentary>(primaryKey, strWhere);
            return new DataAccess().GetPage_List<Model.Documentary>(PageIndex, PageSize, primaryKey, primaryKey + " desc", strWhere);
        }
        public DataTable GetPageDT_Where(int PageIndex, int PageSize, long newsclassid, out int totalCount)
        {
            string strWhere = GetSqlWhere(newsclassid);
            totalCount = new DataAccess().GetCount<Model.Documentary>(primaryKey, strWhere);
            return ConvertHelper<Model.Documentary>.ConvertToDT(new DataAccess().GetPage_List<Model.Documentary>(PageIndex, PageSize, primaryKey, primaryKey + " desc", strWhere));
        }
        public DataTable GetPageDTWhere(int PageIndex, int PageSize, int LotteryChippedid, out int totalCount)
        {
            string strWhere = GetSqlWhere(LotteryChippedid);
            totalCount = new DataAccess().GetCount<Model.Documentary>(primaryKey, strWhere);
            return ConvertHelper<Model.Documentary>.ConvertToDT(new DataAccess().GetPage_List<Model.Documentary>(PageIndex, PageSize, primaryKey, primaryKey + " desc", strWhere));
        }
        /// <summary>
        /// 拼接条件
        /// </summary>
        public string GetSqlWhere(long newsclassid)
        {
            StringBuilder sqlWhere = new StringBuilder();

            sqlWhere.Append("1>0");

            if (newsclassid > 0)
            {
                sqlWhere.Append(" and LotteryType =" + newsclassid);
            }
            return sqlWhere.ToString();
        }
        /// <summary>
        /// 批删
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public int Drop(string Ids)
        {
            return new DataAccess().Drop<Model.Documentary>(Ids, primaryKey);
        }
        public int GetAll_Count(int LotteryChippedID)
        {
            string sqlWhere = "LotteryChippedID=" + LotteryChippedID + "";
            return new Data.DataAccess().GetCount<Model.Documentary>(primaryKey, sqlWhere);
        }
    }
}

